package com.vue.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vue.bean.R;
import com.vue.entity.UserEntity;
import com.vue.entity.po.UserPO;
import com.vue.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: wrj
 * @since: 2023/6/8 17:54
 * @description:
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    @GetMapping("/selectLevel")
    public R selectLevel(){
        try {
            return R.data(3);
        } catch (Exception e) {
           return R.fail("查询异常");
        }
    }

    @GetMapping("/selectPage")
    public R selectPage(UserPO po,
                        @RequestParam("pageNum") Integer pageNum,
                        @RequestParam("pageSize") Integer pageSize){
        log.info("查询列表：{}", JSON.toJSON(po));
        try {
            IPage<UserEntity> page = userService.selectPage(po, pageNum, pageSize);
            return R.data(page);
        } catch (Exception e) {
            return R.fail("暂无数据");
        }
    }

    @GetMapping("/selectList")
    public R selectList(UserPO po){
        log.info("查询列表：{}", JSON.toJSON(po));
        try {
            List<UserEntity> list = userService.selectList(po);
            return R.data(list);
        } catch (Exception e) {
           return R.fail("暂无数据");
        }
    }

    @GetMapping("/selectById")
    public R selectById(@RequestParam("id") Long id){
        log.info("查询用户详情：{}", id);
        try {
            return userService.selectById(id);
        } catch (Exception e) {
            return R.fail("查询用户详情异常");
        }
    }

    @PostMapping("/addOrEditUser")
    public R addUser(@RequestBody UserEntity po){
        log.info("新增用户：{}", JSON.toJSON(po));
        try {
            return userService.addOrEditUser(po);
        } catch (Exception e) {
            return R.fail("新增异常");
        }
    }

    @GetMapping("/deleteById")
    public R deleteById(@RequestParam("id") Long id){
        log.info("删除用户：{}", id);
        try {
            return userService.deleteById(id);
        } catch (Exception e) {
            return R.fail("新增异常");
        }
    }

    @GetMapping("/batchInsert")
    public R batchInsert(@RequestParam("count") Integer count){
        log.info("批量插入数据：{}", count);
        try {
            return userService.batchInsert(count);
        } catch (Exception e) {
            return R.fail("批量插入数据异常");
        }
    }
}


